<?php
namespace App\Service\Front;

use Illuminate\Support\Facades\DB;

class CaseService extends BaseService
{
    //原创案例-导览

    //选项
    public function getCateList($pid = 0)
    {
        $res = $this->cateOption(2, $pid);
        $list = [];
        if (count($res)) {
            foreach ($res as $v) {
                $v = (array) $v;
                $list[] = [
                    'id' => $v['id'],
                    'name' => $v['title'],
                ];
            }
        }
        if (!$pid) {
            $arys[] = [
                'titlename' => '一级分类',
                'groupname' => $list,
            ];
        } else {
            $arys[] = [
                'titlename' => '二级分类',
                'typename' => $list,
            ];
        }
        return $arys;
    }

    //获取子分类IDS
    public function getCateids($pid)
    {
        $ids = [(int)$pid];
        $res = $this->cateOption(2, $pid);
        if (!count($res)) {
            return $ids;
        }
        foreach ($res as $v) {
            $ids[] = $v->id;
        }
        return $ids;
    }

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     */
    public function getIndex($cateid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCase)->where('del', 0);
        if ($cateid) {
            $cateids = $this->getCateids($cateid);
            $db = $db->whereIn('cate_id', $cateids);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $thumb = file_exists($v['thumb']) ? env('DOMAIN').'/'.$v['thumb'] : '';
            //企业信息
            $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            $cname = empty($comOne) ? '-' : $comOne['cname'];
            $clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'intro' => '-',
                'cate_id' => $v['cate_id'],
                'img' => $thumb,
                'author_name' => $cname,
                'author_head' => $clogo,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     * @param int $id
     * @param int $uid
     */
    public function getOne($id, $uid)
    {
        $res = (array) $this->dbConn($this->dbCase)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //企业信息
        $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['cid'])->first();
        $cname = empty($comOne) ? '-' : $comOne['cname'];
        $clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
        //分类信息
        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $res['cate_id'])->first();
        $cateName = empty($cateOne) ? '-' : $cateOne['title'];
        if (substr($res['thumb'], 0, 4) == 'http') {
            $thumb = $res['thumb'];
        } else {
            $thumb = file_exists($res['thumb']) ? env('DOMAIN').'/'.$res['thumb'] : '';
        }
        //关注+点赞+收藏
        $is_follow = $is_like = $is_collect = 0;
        if ($uid) {
            $followOne = (array) $this->dbConn($this->dbCaseFollow)
                ->where('uid', $uid)
                ->where('case_id', $id)
                ->first();
            $is_follow = empty($followOne) ? 0 : 1;
            $likeOne = (array) $this->dbConn($this->dbCaseLike)
                ->where('uid', $uid)
                ->where('case_id', $id)
                ->first();
            $is_like = empty($likeOne) ? 0 : 1;
            $collectOne = (array) $this->dbConn($this->dbCaseCollect)
                ->where('uid', $uid)
                ->where('case_id', $id)
                ->first();
            $is_collect = empty($collectOne) ? 0 : 1;
        }
        $arys = [
            'title' => $res['title'],
            'com_id' => $res['cid'],
            'com_name' => $cname,
            'com_logo' => $clogo,
            'cate_id' => $res['cate_id'],
            'cate_name' => $cateName,
            'address' => $res['address'],
            'buyer_name' => $res['buyer_name'],
            'designer_name' => $res['designer_name'],
            'item_area' => $res['item_area'],
            'main_material' => $res['main_material'] ? implode('，', explode('||||', $res['main_material'])) : '',
            'thumb' => $thumb,
            'item_finish_time' => $res['item_finish_time'],
            'item_detail' => $res['item_detail'] ? htmlspecialchars_decode($res['item_detail']) : '',
            'like_num' => $res['like_num'],
            'collect_num' => $res['collect_num'],
            'forward_num' => $res['forward_num'],
            'is_follow' => $is_follow,
            'is_like' => $is_like,
            'is_collect' => $is_collect,
        ];
        return $this->toback(0, '', $arys);
    }

//    //添加
//    public function add($pas)
//    {
//        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $pas['cate_id'])->first();
//        if (empty($cateOne)) {
//            return $this->toback(-2, '分类不存在');
//        }
//        //数据入库
//        $lastid = $this->dbConn($this->dbCase)->insertGetId($pas);
//        if (!$lastid) {
//            return $this->toback(-1, '发布失败');
//        }
//        return $this->toback();
//    }

    //相关推荐
    public function getRecommendList($cate_id = 0, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCase)->where('del', 0);
        if ($cate_id) {
            $dv = $db->where('cate_id', $cate_id);
        }
        $res = $db->where('is_recommend', 1)
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $cOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            if (substr($v['thumb'], 0, 4) == 'http') {
                $thumb = $v['thumb'];
            } else {
                $thumb = file_exists($v['thumb']) ? env('DOMAIN').'/'.$v['thumb'] : '';
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'cname' => empty($cOne) ? '无' : $cOne['cname'],
                'thumb' => $thumb,
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 点赞设置
     * @param int $uid
     * @param int $caseid
     */
    public function setLike($uid, $caseId)
    {
        $res = (array) $this->dbConn($this->dbCaseLike)
            ->where('uid', $uid)
            ->where('case_id', $caseId)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已点赞');
        }
        $lastid = $this->dbConn($this->dbCaseLike)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '点赞失败');
        }
        return $this->toback();
    }

    /**
     * 关注设置
     * @param int $uid
     * @param int $caseid
     */
    public function setFollow($uid, $caseId)
    {
        $res = (array) $this->dbConn($this->dbCaseFollow)
            ->where('uid', $uid)
            ->where('case_id', $caseId)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已关注');
        }
        $lastid = $this->dbConn($this->dbCaseFollow)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '关注失败');
        }
        return $this->toback();
    }

    /**
     * 收藏设置
     * @param int $uid
     * @param int $caseid
     */
    public function setCollect($uid, $caseId)
    {
        $res = (array) $this->dbConn($this->dbCaseCollect)
            ->where('uid', $uid)
            ->where('case_id', $caseId)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已收藏');
        }
        $lastid = $this->dbConn($this->dbCaseCollect)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '收藏失败');
        }
        return $this->toback();
    }

    /**
     * 转发设置
     * @param int $uid
     * @param int $caseid
     */
    public function setForward($uid, $caseId)
    {
        $res = (array) $this->dbConn($this->dbCaseForward)
            ->where('uid', $uid)
            ->where('case_id', $caseId)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已转发');
        }
        $lastid = $this->dbConn($this->dbCaseForward)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '转发失败');
        }
        return $this->toback();
    }

    /**
     * 举报
     * @param int $uid
     * @param int $caseid
     * @param string $cnt
     */
    public function setJubao($uid, $caseId, $cnt)
    {
        $lastid = $this->dbConn($this->dbCaseJubao)->insertGetId([
            'uid' => $uid, 'case_id' => $caseId, 'cnt' => $cnt,
            'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '举报失败');
        }
        return $this->toback();
    }

    /**
     * 在线咨询
     * @param int $uid
     * @param int $caseid
     * @param string $cnt
     */
    public function onlineAdd($uid, $caseId, $cnt)
    {
        $lastid = $this->dbConn($this->dbCaseOnline)->insertGetId([
            'type' => 1, 'uid' => $uid, 'case_id' => $caseId,
            'cnt' => $cnt, 'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '咨询失败');
        }
        return $this->toback();
    }
}